package com.vcredit.creditcat.time.creditassure;

import java.util.Date;
import java.util.List;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.alibaba.dubbo.common.threadpool.RequestLocalThread;
import com.vcredit.bird.service.foreignTradeCompliance.ForeignTradeComplianceService;
import com.vcredit.bird.vo.foreignTradeCompliance.ComplianceQueryParam;
import com.vcredit.bird.vo.foreignTradeCompliance.ComplianceQueryResponse;
import com.vcredit.creditcat.enumtype.AssureStatusEnum;
import com.vcredit.creditcat.manager.CreditAssureTimaManager;
import com.vcredit.creditcat.model.CreditAssure;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.util.ConcurrentDateUtil;
import com.vcredit.framework.quartz.QuartzJob;

/**
 * 合规查询
 */
@QuartzJob(name = "creditAssureQueryTime", cron = "0 0/10 * * * ?")
public class CreditAssureQueryTime extends QuartzJobBean{

	private Logger logger = LoggerFactory.getLogger(CreditAssureQueryTime.class);
	
	@Autowired
	private  CreditAssureTimaManager creditAssureTimaManager;

	@Autowired
	private ForeignTradeComplianceService foreignTradeComplianceService;
	
	@Autowired
	private ThreadPoolTaskExecutor threadPoolTaskExecutor;
	
	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		logger.info("机构版本征信认证合规查询定时任务执行了................" + ConcurrentDateUtil.formatDateTime(new Date()));
		RequestLocalThread.create();
		List<CreditAssure> caList = creditAssureTimaManager.findToComplianceQuery();
		if (caList == null || caList.isEmpty()){
			return;
		}
		for (CreditAssure ca : caList) {
			threadPoolTaskExecutor.execute(new CrawleThread(ca));
		}
		
	}
	
	class CrawleThread implements Runnable {
		private CreditAssure obj;
		private Logger logger = LoggerFactory.getLogger(CrawleThread.class);

		public CrawleThread(CreditAssure obj) {
			super();
			this.obj = obj;
		}

		public void run() {
			try {
				Cust cust = obj.getCust();
				if(cust==null){
					return;
				}
				ComplianceQueryParam queryParam = new ComplianceQueryParam();
				queryParam.setApplyID(obj.getApplyId());
				queryParam.setIdentityNo(cust.getCustIdno());
				queryParam.setName(cust.getCustName());				
				ComplianceQueryResponse res =foreignTradeComplianceService.complianceQuery(queryParam,obj.getProductId());
				if(res.getIsSeccess()==true && res.getIsSignature()==true){
					CreditAssure newCreditAssure = new CreditAssure();
					newCreditAssure.setAssureId(obj.getAssureId());
					newCreditAssure.setIsSignature(res.getIsSignature().toString());
					newCreditAssure.setSignatureTime(res.getSignatureTime());
					newCreditAssure.setAssureStatus(AssureStatusEnum.Query.getCode());
					creditAssureTimaManager.updateAssure(newCreditAssure);
				}
			} catch (Exception e) {
				logger.error("CreditAssureApplyTime error:", e);
			}
		}
	}

}
